package cn.com.text;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.bezierlayout.BezierLayout;

import org.litepal.crud.DataSupport;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cn.com.text.Activity.LogActivity;
import cn.com.text.Activity.MapActivity;
import cn.com.text.Activity.MyPostActivity;
import cn.com.text.Activity.PublishPost;
import cn.com.text.Activity.WebBlog;
import cn.com.text.Dao.MyPost;
import cn.com.text.Dao.News;
import cn.com.text.Dao.User;
import cn.com.text.Util.ImageHelper;
import cn.com.text.Util.MySQLConnections;

import static android.content.ContentValues.TAG;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    private boolean is_online = false;
    private static Connection conn = null;
    private static PreparedStatement stmt = null;
    private SwipeRefreshLayout swipeRefreshLayout;
    private List<News> sNewsList = new ArrayList<News>();
    private ImageView imageViewUserImage;
    private String userName = null;
    private String passWord = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("主页");
        setSupportActionBar(toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        imageViewUserImage = (ImageView) findViewById(R.id.userImageLoged);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            if(is_online){
                Intent intent = new Intent(MainActivity.this, PublishPost.class);
                intent.putExtra("userName",userName);
                startActivity(intent);
                item.setIntent(intent);
            }else {
                Toast.makeText(MainActivity.this,"请先登录",Toast.LENGTH_SHORT).show();
            }

        }
        return super.onOptionsItemSelected(item);
    }
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        if (id == R.id.nav_camera) {
            if(is_online == true){
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, PublishPost.class);
                intent.putExtra("userName",userName);
                Log.d("PostUsername:",userName);
                startActivity(intent);
            }else {
                Toast.makeText(MainActivity.this,"请先登录",Toast.LENGTH_SHORT).show();
            }
        } else if (id == R.id.nav_gallery) {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, WebBlog.class);
            startActivity(intent);
        } else if (id == R.id.nav_slideshow) {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, LogActivity.class);
            startActivityForResult(intent,3);
        } else if (id == R.id.nav_manage) {
            Intent intent = new Intent(MainActivity.this,MapActivity.class);
            startActivity(intent);
        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }
        return true;
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String postTitle = data.getStringExtra("postTitle");
                    userName = data.getStringExtra("UNAME");
                }
                break;
            case 3:
                if(resultCode == RESULT_OK){
                    userName = data.getStringExtra("username");
                    passWord = data.getStringExtra("password");
                    //String imageTouXiang = getIntent().getStringExtra("image");
                    Log.d("Username : ",userName);
                    //Log.d("Password : ",passWord);
                    List<User> userList = DataSupport.where("name = ? and password = ?",userName,passWord).find(User.class);
                    String imageTouXiang = "";
                    if(userList.size() == 1){
                        User user = userList.get(0);
                        imageTouXiang = user.getImage();
                    }
                    is_online = true;
                   // Log.d("Touxiang",imageTouXiang);
                    imageViewUserImage = (ImageView) findViewById(R.id.userImageLoged);
                    imageViewUserImage.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(is_online){
                                Intent intent = new Intent(MainActivity.this, MyPostActivity.class);
                                intent.putExtra("userName",userName);
                                startActivity(intent);
                            }

                        }
                    });
                    if(imageTouXiang.equals("")){
                        imageViewUserImage.setImageResource(R.drawable.touxiang);
                    }else {
                        Bitmap bitmap = ImageHelper.stringToBitmap(imageTouXiang);
                        imageViewUserImage.setImageBitmap(bitmap);
                    }
                }

        }
    }
}
